import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { join } from 'path';
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
import { ValidationPipe } from './common-middleware/pipe/validate.pipe';
import { RequestInterceptor } from './common-middleware/interceptor/request.interceptor';
import { LoggerMiddleware } from './common-middleware/middleware/logger.middleware';
import { ResponseInterceptor } from './common-middleware/interceptor/response.interceptor';
import { JwtAuthGuard } from './common-middleware/guard/jwt-auth.guard';
import { CustomExceptionFilter } from './common-middleware/filter/exception.filter';

// 数据库
import { MongodbModule } from './database/mongodb/mongodb.module';
import { MysqlModule } from './database/mysql/mysql.module';
import { RedisModule } from './database/redis/redis.module';

// 公共模块
import { CronTaskModule } from './modules/com-cron-task/cron-task.module';
import { AuthModule } from './modules/com-auth/auth.module';
import { SseModule } from './modules/com-sse/sse.module';
import { ComMinioModule } from './modules/com-minio/com-minio.module';
import { ComExcelToolModule } from './modules/com-excel-tool/com-excel-tool.module';
import { ComLocalUploadModule } from './modules/com-local-upload/com-local-upload.module';

// 业务模块
import { UserModule } from './modules/sys-user/user.module';
import { DepartModule } from './modules/sys-depart/depart.module';
import { RoleModule } from './modules/sys-role/role.module';
import { ScjcModule } from './modules/test-scjc/scjc.module';
import { SysMenuModule } from './modules/sys-menu/menu.module';
import { CatModule } from './modules/test-cat-mysql/cat.module';
import { DogModule } from './modules/test-dog-mysql/dog.module';

import * as dotenv from 'dotenv';

// 动态选择加载的环境配置文件
const envFilePath = `.env.${process.env.NODE_ENV || 'development'}`;

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 使配置对所有模块可用
      envFilePath: envFilePath, // 加载对应环境的配置文件
      load: [() => dotenv.config({ path: '.env' })], // 加载共享配置文件
    }),
    MysqlModule,
    MongodbModule,
    RedisModule.forRoot(),

    // 公共模块
    AuthModule,
    SseModule,
    CronTaskModule,
    ComExcelToolModule,
    ComMinioModule,
    ComLocalUploadModule,

    // 业务模块
    UserModule,
    DepartModule,
    RoleModule,
    SysMenuModule,
    ScjcModule,

    CatModule,
    DogModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    { provide: APP_FILTER, useClass: CustomExceptionFilter }, // 过滤器 - 全局异常过滤器
    { provide: APP_PIPE, useClass: ValidationPipe }, // 管道 - http入参参数校验管道
    { provide: APP_INTERCEPTOR, useClass: RequestInterceptor }, // 拦截器 - 入参参数转换拦截器
    { provide: APP_INTERCEPTOR, useClass: ResponseInterceptor }, // 拦截器 - 响应参数转换拦截器
    { provide: APP_GUARD, useClass: JwtAuthGuard }, // 守卫 - jwt验证守卫],
  ],
})
export class AppModule {
  // 应用中间件
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware) // 在此处可添加多个中间件
      // 在此处可添加多个路径或controller不经过该中间件
      .exclude({ path: 'scjc/getAllData', method: RequestMethod.ALL })
      // .exclude({ path: 'test-server', method: RequestMethod.ALL }) // 在此处可添加多个路径或controller不经过该中间件
      .forRoutes({ path: '*', method: RequestMethod.ALL }); // 选择需要进入改中间件的路径以及请求方法
  }
}
